﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.Globalization.Utilities;

namespace Medianamik.UI.Web.ModalWindows
{
    public partial class ExportResourcesWindow : BasePage
    {
        private readonly MedianamikResourceDataManager _manager = new MedianamikResourceDataManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var resourceType = IsCurrentUserSuperAdmin ? ResourceListingTypes.AllResources : ResourceListingTypes.NonSecuredOnly;

                var resourceSets = _manager.GetAllResourceSets(resourceType);
                ResourceSets.DataSource = resourceSets;
                ResourceSets.DataValueField = "ResourceSet";
                ResourceSets.DataBind();
            }
        }

        protected void ExportSql(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var checkedValues = ResourceSets.GetCheckedValues();
                var exportResult = new SqlResourceExporter().Export(checkedValues, _manager);
                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment: inline; filename=SqlExport.sql");
                Response.AppendHeader("Content-Length", Encoding.UTF8.GetByteCount(exportResult.ToString()).ToString());

                Response.Write(exportResult.ToString());
                Response.End();
            }
        }

        protected void ValidateCheckBoxes(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = ResourceSets.SelectedIndex != -1;
        }

        protected void SelectAll(object sender, EventArgs e)
        {
            var chk = SelectAllBox.Checked;
            foreach (ListItem item in ResourceSets.Items)
            {
                item.Selected = chk;
            }
        }

        protected void btnExportToExcel_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var checkedValues = ResourceSets.GetCheckedValues();
                var excelExporter = new ExcelResourceExporter();
                excelExporter.LanguagesToExport = ConfigManager.GetEditionCultures()
                    .Select(x => x.CultureName);

                var excelDoc = excelExporter.Export(checkedValues, _manager);

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentEncoding = Encoding.Default;
                Response.AddHeader("content-disposition", "attachment; filename=ResourceSetsExport.xml");
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Pragma", "public");

                excelDoc.Save(Response.OutputStream);
                Response.End();
            }
        }
    }

}
